// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.viewers.folder;

import java.util.List;

import org.dengues.commons.utils.DenguesTextUtil;
import org.dengues.core.resource.WarehouseResourceFactory;
import org.dengues.core.warehouse.ENodeCategoryName;
import org.dengues.core.warehouse.ENodeStatus;
import org.dengues.designer.ui.DesignerUIPlugin;
import org.dengues.warehouse.i18n.Messages;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2008-1-7 qiang.zhang $
 * 
 */
public class NewFolderWizardPage extends WizardPage {

    private IStatus nameStatus;

    private final IPath path;

    private final ENodeCategoryName nodeCategoryName;

    private String origname;

    /**
     * qzhang NewProcessWizardPage constructor comment.
     * 
     * @param nodeCategoryName
     * 
     * @param pageName
     */
    public NewFolderWizardPage(ENodeCategoryName nodeCategoryName, IPath path) {
        super(Messages.getString("NewDatabaseWizardPage.0")); //$NON-NLS-1$
        nameStatus = createOkStatus();
        this.path = path;
        this.nodeCategoryName = nodeCategoryName;
        setTitle(Messages.getString("CreateFolderAction.createFolder"));//$NON-NLS-1$
        setDescription(Messages.getString("CreateFolderAction.createFolder"));//$NON-NLS-1$
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com NewFolderWizardPage constructor comment.
     * 
     * @param nodeCategoryName2
     * @param path2
     * @param origname
     */
    public NewFolderWizardPage(ENodeCategoryName nodeCategoryName2, IPath path2, String origname) {
        this(nodeCategoryName2, path2);
        this.origname = origname;
        setTitle(Messages.getString("NewFolderWizard.editPro"));//$NON-NLS-1$
        setDescription(Messages.getString("NewFolderWizard.editPro"));//$NON-NLS-1$

    }

    private Text nameText;

    private String folderName;

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
     */
    public void createControl(Composite parent) {
        Composite container = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout(2, false);
        container.setLayout(layout);
        Label nameLab = new Label(container, SWT.NONE);
        nameLab.setText(Messages.getString("NewDatabaseWizardPage.Label.Name")); //$NON-NLS-1$

        nameText = new Text(container, SWT.BORDER);
        nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        if (origname != null) {
            nameText.setText(origname);
        }
        setControl(container);
        setPageComplete(false);
        addListeners();
        checkFieldsValue();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "checkFieldsValue".
     */
    private void checkFieldsValue() {
        if (nameText.getText().length() == 0) {
            nameStatus = new Status(IStatus.ERROR, DesignerUIPlugin.PLUGIN_ID, IStatus.ERROR, Messages
                    .getString("NewFolderWizardPage.foldernam.empty"), null); //$NON-NLS-1$
        } else {
            if (isFolderNameAlreadyUsed(nameText.getText())) {
                nameStatus = new Status(IStatus.ERROR, DesignerUIPlugin.PLUGIN_ID, IStatus.ERROR, Messages
                        .getString("NewFolderWizardPage.folder.exist"), //$NON-NLS-1$
                        null);
            } else {
                nameStatus = createOkStatus();
            }
        }
        setMessage(nameStatus);
        boolean b = nameStatus.getSeverity() != IStatus.ERROR;
        setPageComplete(b);
        if (b) {
            folderName = nameText.getText();
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "isDatabaseNameAlreadyUsed".
     * 
     * @param text
     * @return
     */
    private boolean isFolderNameAlreadyUsed(String text) {
        List<String> names = WarehouseResourceFactory.getSubFolderNames(nodeCategoryName, path);
        return names.contains(text);
    }

    /**
     * zx Comment method "addListeners".
     */
    private void addListeners() {
        nameText.addModifyListener(new ModifyListener() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
             */
            public void modifyText(ModifyEvent e) {
                checkFieldsValue();
            }

        });
    }

    public String getFolderName() {
        return DenguesTextUtil.getNotNullString(folderName);
    }

    public String getDatabaseStatus() {
        return ENodeStatus.NORMAL.getId();
    }

    private void setMessage(IStatus status) {
        if (IStatus.ERROR == status.getSeverity()) {
            setErrorMessage(status.getMessage());
            setMessage(""); //$NON-NLS-1$
        } else {
            setMessage(status.getMessage());
            setErrorMessage(null);
        }
    }

    private static IStatus createOkStatus() {
        return new Status(IStatus.OK, DesignerUIPlugin.PLUGIN_ID, IStatus.OK, "", null); //$NON-NLS-1$
    }
}
